package com.oc.mba.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.oc.mba.entity.MessageEntity;
import com.oc.mba.entity.OcBackendUser;
import com.oc.mba.entity.OcPaper;
import com.oc.mba.entity.OcPaperQuestion;
import com.oc.mba.entity.OcQuestion;
import com.oc.mba.entity.OcQuestionOption;
import com.oc.mba.entity.OcQuestionSeckill;
import com.oc.mba.entity.OcQuestionSolution;
import com.oc.mba.service.BackendService;
import com.oc.mba.service.MessageService;
import com.oc.mba.service.PaperService;
import com.oc.mba.util.CommConsts;
import com.oc.mba.util.CommLog;
import com.oc.mba.util.CommUtil;
import com.oc.mba.util.JsonUtil;
import com.oc.mba.util.PaginationSupport;
import com.oc.mba.util.ResultCode;
import com.oc.mba.util.SpringUtil;
 
/**
 * 后台管理
 * @author unidw
 *
 */
public class BackendController extends BaseBackendController {
	
	private static final CommLog logger = new CommLog(BackendController.class);
	
	/**
	 * 后台Service
	 */
	private BackendService backendService = (BackendService) SpringUtil.getBean("backendServiceImpl");
	
	/**
	 * 试卷Service
	 */
	private PaperService paperService = (PaperService) SpringUtil.getBean("paperServiceImpl");
	
	/**
	 * 登陆
	 */
	public ModelAndView login(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for login");
		
		if (hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do");
	        return mav;
		}
		
		String userName = httpRequest.getParameter("admin_name");
		String password = httpRequest.getParameter("admin_password");
		logger.info("userName=" + userName + ", password=" + password);
		OcBackendUser user = backendService.findUser(userName, password);
		if (user != null) {
			logger.info("backend login success.");
			setSession(user, httpRequest, httpResponse);
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do");
	        return mav;
		}else {
			logger.info("backend login failure.");
			// TODO message
			ModelAndView mav = new ModelAndView();
			mav.addObject("errorMessage", "user not exist.");
	        mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
       
    }
	
	/**
	 * 登出
	 */
	public ModelAndView logOut(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for login out");
		
		if (hasSession(httpRequest, httpResponse)) {
			removeSession(httpRequest, httpResponse);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=initLogin");
        return mav;
    }
	

	/**
	 * 初始化登陆
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView initLogin(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		logger.info("backend for initLogin");
        ModelAndView mav = new ModelAndView();
        
        mav.setViewName("backend/login");
        return mav;
    }
	
	/**
	 * 试卷列表
	 * @param httpRequest
	 * @param httpResponse
	 * @return
	 * @throws Exception
	 */
	public ModelAndView questionModule(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for questionModule");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		
		int toPage = 0;
		String toPageStr = httpRequest.getParameter("toPage");
		if (toPageStr != null && !toPageStr.equals("")) {
			toPage = Integer.valueOf(toPageStr);
		}
		
		PaginationSupport paginationSupport = paperService.listAllQuestionByPaging(toPage);
		paginationSupport.setActionUrl("backend.do?action=questionModule");
		ModelAndView mav = new ModelAndView();
		mav.addObject("type", "2");
		mav.addObject("pagination", paginationSupport);
        mav.setViewName("backend/question_list");
        return mav;
	}
	
	/**
	 * 修改问题选项为正确
	 * @param httpRequest
	 * @param httpResponse
	 * @return
	 * @throws Exception
	 */
	public ModelAndView modifyQuestionOptionCorrect(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for view");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		String questionIDStr = httpRequest.getParameter("questionID");
		Integer questionID = Integer.valueOf(questionIDStr);
		
		String optionIDStr = httpRequest.getParameter("optionID");
		Integer optionID = Integer.valueOf(optionIDStr);
		
		paperService.updateQuestionOptionCorrect(optionID);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 修改问题选项为错误
	 * @param httpRequest
	 * @param httpResponse
	 * @return
	 * @throws Exception
	 */
	public ModelAndView modifyQuestionOptionFault(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for view");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		String questionIDStr = httpRequest.getParameter("questionID");
		Integer questionID = Integer.valueOf(questionIDStr);
		
		String optionIDStr = httpRequest.getParameter("optionID");
		Integer optionID = Integer.valueOf(optionIDStr);
		
		paperService.updateQuestionOptionFault(optionID);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 试卷列表
	 * @param httpRequest
	 * @param httpResponse
	 * @return
	 * @throws Exception
	 */
	public ModelAndView view(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for view");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		List<OcPaper>  papers = paperService.listAll();
		
        ModelAndView mav = new ModelAndView();
        mav.addObject("type", "1");
        mav.addObject("papars", papers);
        mav.setViewName("backend/paper_list");
        return mav;
    }
	
	/**
	 * 初始化添加试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView initAddPaper(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for initAddPaper");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
        ModelAndView mav = new ModelAndView();
        
        mav.setViewName("backend/add_paper");
        return mav;
	}
	
	/**
	 * 初始化添加试卷
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView addPaper(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for addPaper");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String paperName = httpRequest.getParameter("paper_name");
		String paperDesc = httpRequest.getParameter("paper_desc");
		String paperPrice = httpRequest.getParameter("paper_price");
		
		logger.info("paperName=" + paperName + ", paperDesc=" + paperDesc + ", paperPrice=" + paperPrice);
		
		OcPaper paper = new OcPaper();
		paper.setPaperDesc(paperDesc);
		paper.setPaperTitle(paperName);
		paper.setPaperPrice(Double.valueOf(paperPrice));
		paper.setPaperStatus(0);
		paper.setPaperType(0);
		paper.setPaperVersion(0);
		paperService.save(paper);
		ModelAndView mav = new ModelAndView();
	        
		mav.setViewName("redirect:backend.do");
		return mav;
	}
	
	/**
	 * 添加试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView viewQuestion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for viewQuestion");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		OcQuestion question = null;
		try {
			String questionIDStr = httpRequest.getParameter("question_id");
			// TODO check questionID
			Integer questionID = Integer.valueOf(questionIDStr);
			logger.info("questionSerial=" + questionIDStr);
			question = paperService.findQuestion(questionID);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("type", "2");
		mav.addObject("question", question);
		mav.setViewName("backend/question_view");
		return mav;
	}
	/**
	 * 添加试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView addQuestion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for addQuestion");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionSerial = httpRequest.getParameter("question_serial");
		String questionTag = httpRequest.getParameter("question_tag");
		String questionTitle = httpRequest.getParameter("question_title");
		String cPage = httpRequest.getParameter("cPage");
		logger.info("questionSerial=" + questionSerial + ", questionTag=" + questionTag + ", questionTitle=" + questionTitle + ", cPage=" + cPage);
		
		OcQuestion question = new OcQuestion();
		question.setQuestionSerial(questionSerial);
		question.setQuestionTag(questionTag);
		question.setQuestionTitle(questionTitle);
		question.setQuestionType(1);
		question.setQuestionIndex(1);
		question.setQuestionStatus((short)0);
		question.setQuestionSummary(CommUtil.getMaxString(30, questionTitle));
		paperService.save(question);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=questionModule&toPage=" + cPage);
		return mav;
	}
	
	/**
	 * 发布试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView publishQuestion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for publishQuestion");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("question_id");
		Integer publishFlag = Integer.valueOf(httpRequest.getParameter("flag"));
		String cPage = httpRequest.getParameter("cPage");
		int questionID = 0;
		// TODO check questionID
		questionID = Integer.valueOf(questionIDStr);
		
		logger.info("questionSerial=" + questionIDStr + ", flag=" + publishFlag + ", cPage=" + cPage);
		if (publishFlag == CommConsts.State.PUBLISH) {
			paperService.publishQuestion(questionID);
		}
		else {
			paperService.unpublishQuestion(questionID);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=questionModule&toPage=" + cPage);
		return mav;
	}
	
	/**
	 * 发布试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView deleteQuestion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for publishQuestion");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("question_id");
		String cPage = httpRequest.getParameter("cPage");
		int questionID = 0;
		// TODO check questionID
		questionID = Integer.valueOf(questionIDStr);
		logger.info("questionID=" + questionIDStr + ", cPage=" + cPage);
		paperService.deleteQuestion(questionID, CommConsts.LOGIC_DELETE);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=questionModule&toPage=" + cPage);
		return mav;
	}
	
	
	/**
	 * 删除试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView delPaper(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for addPaper");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		Integer paperID = Integer.valueOf(httpRequest.getParameter("paperID"));
		paperService.deletePaper(paperID);
		ModelAndView mav = new ModelAndView();
        
		mav.setViewName("redirect:backend.do");
		return mav;
	}
	
	/**
	 * 查看试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView viewPaper(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for viewPaper");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		Integer paperID = Integer.valueOf(httpRequest.getParameter("paperID"));
		// 取得试卷
		OcPaper paper = paperService.findPaper(paperID);
		// 取得试卷的试题列表
		List<OcQuestion> questionList = paperService.findQuestionByPaperID(paperID);
		ModelAndView mav = new ModelAndView();
		mav.addObject("paper", paper);
		mav.addObject("type", "1");
		mav.addObject("questionList", questionList);
		mav.setViewName("backend/paper_view");
		return mav;
	}
	
	/**
	 * 发布/取消发布试题
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView publishPaper(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for publish Paper");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		Integer publishFlag = Integer.valueOf(httpRequest.getParameter("flag"));
		Integer paperID = Integer.valueOf(httpRequest.getParameter("paperID"));
		OcPaper paper = paperService.findPaper(paperID);
		if (publishFlag == CommConsts.State.PUBLISH) {
			paper.setPaperStatus(CommConsts.State.PUBLISH);
		}
		else {
			paper.setPaperStatus(CommConsts.State.UNPUBLISH);
		}
		
		paperService.update(paper);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do");
		return mav;
	}
	
	/**
	 * @brief 为试卷添加试题
	 * @param httpRequest
	 * @param httpResponse
	 * @return
	 * @throws Exception
	 */
	public ModelAndView addPaperQuestion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for add Paper Question");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		Integer paperID = Integer.valueOf(httpRequest.getParameter("paper_ID"));
		String questionIDs = httpRequest.getParameter("question_IDs");
		logger.info("paperID=" + paperID);
		logger.info("questionIDs=" + questionIDs);
		OcPaper paper = paperService.findPaper(paperID);
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		if (questionIDs!=null && !questionIDs.equals("") && paper!=null) {
			String qIDs[] = questionIDs.split(",");
			for (String qID : qIDs) {
				OcPaperQuestion paperQuestion = new OcPaperQuestion();
				paperQuestion.setOcPaper(paper);
				paperQuestion.setQuestionId(Integer.valueOf(qID));
				
				paperQuestion.setCreateTime(timestamp);
				paperQuestion.setUpdateTime(timestamp);
				paperService.savePaperQuestion(paperQuestion);
			}
		}
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewPaper&paperID=" + paperID);
		return mav;
	}
	
	/**
	 * 删除试卷问题关联
	 * @param httpRequest
	 * @param httpResponse
	 * @return
	 * @throws Exception
	 */
	public ModelAndView removePaperQuestion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception {
		logger.info("backend for remove Paper Question");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		Integer paperID = Integer.valueOf(httpRequest.getParameter("paperID"));
		Integer questionID = Integer.valueOf(httpRequest.getParameter("questionID"));
		try {
			paperService.removePaperQuestion(paperID, questionID);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("remove Paper Question error.");
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewPaper&paperID=" + paperID);
		return mav;
	}
	
	/**
	 * AJAX方式查询问题
	 * @param request 请求
	 * @param response 响应
	 * @throws Exception 异常
	 */
	public void ajaxQuestion(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) throws Exception {
		// ajax功能分发标识
		String flag = httpRequest.getParameter("flag");
		// 查询试题列表
		if (flag.equals("0")) {
			searchQuestion(httpRequest, httpResponse);
		}
		// 添加问题选项
		else if (flag.equals("1")) {
			addQuestionOption(httpRequest, httpResponse);
		}
	}

	/**
	 * 添加问题选项
	 * @param httpRequest
	 * @param httpResponse
	 */
	public ModelAndView addQuestionOption(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) {
		logger.info("backend for add Question Option");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("question_id");
		Integer questionID = Integer.valueOf(questionIDStr);
		String optionContent = httpRequest.getParameter("optionContent");
		Short optionCorrect = Short.valueOf(httpRequest.getParameter("optionCorrect"));
		OcQuestion question = paperService.findQuestion(questionID);
		OcQuestionOption option = new OcQuestionOption();
		option.setOcQuestion(question);
		option.setOptionContent(optionContent);
		option.setOptionCorrect((short)optionCorrect);
		paperService.save(option);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 添加问题选项
	 * @param httpRequest
	 * @param httpResponse
	 */
	public ModelAndView addQuestionSolution(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) {
		logger.info("backend for add Question Solution");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("question_id");
		Integer questionID = Integer.valueOf(questionIDStr);
		String solutionContent = httpRequest.getParameter("solutionContent");
		
		OcQuestion question = paperService.findQuestion(questionID);
		OcQuestionSolution solution = new OcQuestionSolution();
		solution.setOcQuestion(question);
		solution.setSolutionContent(solutionContent);
		paperService.save(solution);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 添加问题选项
	 * @param httpRequest
	 * @param httpResponse
	 */
	public ModelAndView addQuestionSeckill(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) {
		logger.info("backend for add Question Seckill");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("question_id");
		Integer questionID = Integer.valueOf(questionIDStr);
		String seckillContent = httpRequest.getParameter("seckillContent");
		OcQuestion question = paperService.findQuestion(questionID);
		OcQuestionSeckill seckill = new OcQuestionSeckill();
		seckill.setOcQuestion(question);
		seckill.setSeckillContent(seckillContent);
		paperService.save(seckill);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 添加问题选项
	 * @param httpRequest
	 * @param httpResponse
	 */
	public ModelAndView deleteQuestionOption(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) {
		logger.info("backend for add Question Option");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("questionID");
		Integer questionID = Integer.valueOf(questionIDStr);
		
		String optionIDStr = httpRequest.getParameter("optionID");
		Integer optionID = Integer.valueOf(optionIDStr);
		
		paperService.deleteQuestionOption(optionID);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 添加问题选项
	 * @param httpRequest
	 * @param httpResponse
	 */
	public ModelAndView deleteQuestionSolution(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) {
		logger.info("backend for add Question Solution");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("questionID");
		Integer questionID = Integer.valueOf(questionIDStr);
		
		String solutionIDStr = httpRequest.getParameter("solutionID");
		Integer solutionID = Integer.valueOf(solutionIDStr);
		
		paperService.deleteQuestionSolution(solutionID);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}
	
	/**
	 * 添加问题选项
	 * @param httpRequest
	 * @param httpResponse
	 */
	public ModelAndView deleteQuestionSeckill(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse) {
		logger.info("backend for add Question Seckill");
		if (!hasSession(httpRequest, httpResponse)) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("redirect:backend.do?action=initLogin");
	        return mav;
		}
		
		String questionIDStr = httpRequest.getParameter("questionID");
		Integer questionID = Integer.valueOf(questionIDStr);
		
		String seckillIDStr = httpRequest.getParameter("seckillID");
		Integer seckillID = Integer.valueOf(seckillIDStr);
		
		paperService.deleteQuestionSeckill(seckillID);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:backend.do?action=viewQuestion&question_id=" + questionID);
		return mav;
	}

	/**
	 * 查询试题
	 * @param httpRequest
	 * @param httpResponse
	 * @throws IOException
	 */
	private void searchQuestion(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
		String serial = httpRequest.getParameter("serial");
		if (serial==null || serial.equals("")) {
			logger.error("backend for ajaxQuestion. invalid serial");
			resultJson(httpResponse, genError(ResultCode.INVALID_REQ));
			return;
		}
		List<OcQuestion> questions = paperService.findQuestionBySerial(serial);
		List<Map<String, Object>> questionList = new ArrayList<Map<String, Object>>();
		for (OcQuestion ocQuestion : questions) {
			Map<String, Object> questionMap = new HashMap<String, Object>();
			questionMap.put("id", ocQuestion.getId());
			questionMap.put("questionSummary", ocQuestion.getQuestionSummary());
			questionList.add(questionMap);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(CommConsts.RESULT_FLAG, ResultCode.SUCCESS);
		result.put("questionsSize", questionList.size());
		result.put("questions", questionList);
		resultJson(httpResponse, JsonUtil.jsonMTS(result));
	}
	
	/**
	 * 生成Error的JSON格式字符
	 */
	protected String genError(int errorCode) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(CommConsts.RESULT_FLAG, errorCode);
		return JsonUtil.jsonMTS(result);
	}
	
	/**
	 * 返回JSON格式数据
	 */
	protected void resultJson(HttpServletResponse response, String result) throws IOException {
		response.getWriter().print(result);
	}
}